package com.example.sprintbootarithmetic.DataStructure.doubleLinkedlist;

public class DoubleLinklist {
    public static HeroNode head = new HeroNode(0, "", "");

    /**
     * 方法描述
     *
     * @param node
     * @description:双链表添加的方法
     * @return: void
     * @author: daixingyu
     * @date: 2021-05-03  8:37
     */
    public void HeroNodeadd(HeroNode node) {
        HeroNode nodes = head;
        while (true) {
            if (nodes.next == null) {
                nodes.next = node;
                node.prev = nodes;
                return;
            }
            nodes = nodes.next;
        }
    }

    /**
     * 方法描述
     *
     * @param node
     * @description:顺序添加
     * @return: void
     * @author: daixingyu
     * @date: 2021-05-03  19:47
     */
    public void addbyOrder(HeroNode node) {
        HeroNode nodes = head;
        boolean flag = false;
        while (true) {
            if (nodes.next == null) {
                break;
            } else if (nodes.next.getId() > node.getId()) {
                break;
            } else if (nodes.next.getId() == node.getId()) {
                flag = true;
                break;
            }
            nodes = nodes.next;
        }
        if (flag) {
            System.out.println("对不起，该节点已在链表中存在");
        } else {
            node.prev = nodes.prev;
            node.next = nodes.next;
            nodes.next = node;
        }
    }

    /**
     * 方法描述
     *
     * @param unode
     * @description:修改双链表的内容
     * @return: void
     * @author: daixingyu
     * @date: 2021-05-03  13:35
     */
    public void HeroNodeupdate(HeroNode unode) {
        HeroNode temp = head.next;
        if (temp == null) {
            System.out.println("对不起，该链表为空");
            return;
        }
        boolean flag = false;
        while (true) {
            if (temp.getId() == unode.getId()) {
                flag = true;
                break;
            } else if (temp == null) {
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.setName(unode.getName());
            temp.setNickname(unode.getNickname());
        } else {
            System.out.println("对不起，没有找到该节点");
        }
    }

    /**
     * 方法描述
     *
     * @param id
     * @description:双链表的删除
     * @return: void
     * @author: daixingyu
     * @date: 2021-05-03  8:53
     */
    public void del(int id) {
        HeroNode nodedemo = head.next;
        while (true) {
            if (nodedemo.next != null && nodedemo.getId() == id) {
                break;
            }
            nodedemo = nodedemo.next;
        }
        nodedemo.prev.next = nodedemo.next;
        if (nodedemo.next != null) {
            nodedemo.next.prev = nodedemo.prev;
        }
    }

    /**
     * 方法描述
     *
     * @param
     * @description:遍历双链表
     * @return: void
     * @author: daixingyu
     * @date: 2021-05-03  9:06
     */
    public void listItem() {
        if (head.next == null) {
            System.out.println("对不起，该链表为空");
            return;
        }
        HeroNode temp = head.next;
        while (true) {
            if (temp == null) {
                return;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }
}
